package br.com.acs.CameraIP.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.acs.CameraIP.controller.LoginController;
import br.com.acs.CameraIP.model.Perfil;
import br.com.acs.CameraIP.util.HibernateUtil;


public class PerfilDaoImp implements PerfilDao {
    LoginController login = new LoginController();
    private long idEmpresa = login.PegarClienteLogado();
 
    public void save(Perfil perfil) {
    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction t = session.beginTransaction();
        perfil.setIdCliente(idEmpresa);
        session.merge(perfil);
        t.commit();
    }
    public Perfil getPerfil(long id) {
    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    	Transaction t = session.beginTransaction();
    	t.commit();
        return (Perfil) session.load(Perfil.class, id);
    }
    @SuppressWarnings("unchecked")
    public List<Perfil> listTodos() {
    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction t = session.beginTransaction();
		List<Perfil> lista = session.createQuery("from Perfil perfil where perfil.idEmpresa = :idEmpresa order by perfil.descricaoPerfil").setParameter("idEmpresa",idEmpresa).list();
        t.commit();
        return lista;
    }
    @SuppressWarnings("unchecked")
    public List<Perfil> listAtivos() {
    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction t = session.beginTransaction();
		List<Perfil> lista = session.createQuery("from Perfil perfil where perfil.ativo = 'S' and perfil.idEmpresa = :idEmpresa order by perfil.descricaoPerfil").setParameter("idEmpresa",idEmpresa).list();
        t.commit();
        return lista;
    }
    
    @SuppressWarnings("unchecked")
	public List<Perfil> listPorUfAtivos(Long idUf) {
    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction t = session.beginTransaction();        
        List<Perfil> lista = session.createQuery("from Perfil perfil where perfil.ativo = 'S' and perfil.idUf= :idUf and perfil.idEmpresa= :idEmpresa order by perfil.nome").setParameter("idUf",idUf).setParameter("idEmpresa",idEmpresa).list();
        t.commit();
        return lista;
    } 
       
    public void remove(Perfil perfil) {
    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction t = session.beginTransaction();
        session.delete(perfil);
        t.commit();
    }
    public void update(Perfil perfil) {
    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction t = session.beginTransaction();
        perfil.setIdCliente(idEmpresa);
        session.update(perfil);
        t.commit();
    }
}
